package xyz.thoughtset.viewer.modules.api.controller;


import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.write.metadata.WriteSheet;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import xyz.thoughtset.viewer.common.api.controller.BaseController;
import xyz.thoughtset.viewer.common.core.constants.FolderNameConstant;
import xyz.thoughtset.viewer.modules.api.constants.ApiParamTypeConstant;
import xyz.thoughtset.viewer.modules.api.entity.ApiInfo;
import xyz.thoughtset.viewer.modules.api.entity.ApiParam;
import xyz.thoughtset.viewer.modules.api.service.ApiInfoService;
import xyz.thoughtset.viewer.modules.excel.entity.ExportDataInfo;
import xyz.thoughtset.viewer.modules.excel.service.ExportDataInfoService;
import xyz.thoughtset.viewer.modules.step.executor.SimpleBlockExecutor;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


public class BaseApiController extends BaseController {

    @Autowired
    protected ApiInfoService apiInfoService;
    @Autowired
    protected SimpleBlockExecutor simpleBlockExecutor;
    @Autowired
    protected ExportDataInfoService exportDataService;


    protected Map doQuery(LinkedHashMap<String, Object> searchMap, ApiInfo apiInfo) {
        if (apiInfo == null) {
            throw new NullPointerException("api not found");
        }
        Map<Integer,List<ApiParam>> apiParamMap = apiInfo.groupParams();
        Map paramMap = beforeQuery(searchMap, apiInfo,apiParamMap);
        Map result = simpleBlockExecutor.executeQuery(apiInfo.getId(),paramMap);
        return afterQuery(result, apiInfo,apiParamMap);
    }

    protected Map beforeQuery(LinkedHashMap<String, Object> searchMap, ApiInfo apiInfo, Map<Integer,List<ApiParam>> apiParamMap) {
        if (apiInfo == null) {
            throw new NullPointerException("api not found");
        }
        //过滤入参+临时变量
        Map paramMap = new HashMap<>();
        List<ApiParam> inputParams = apiParamMap.get(ApiParamTypeConstant.INPUT);
        if(!ObjectUtils.isEmpty(inputParams) && !ObjectUtils.isEmpty(searchMap)){
            paramMap = new LinkedHashMap<>();
            for(ApiParam param : inputParams) {
                String key = param.getTitle();
                Object paramValue = searchMap.get(key);
                if (paramValue == null) {
                    paramValue = param.getDefaultVal();
                }
                paramMap.put(key, paramValue);
            }
        }
        List<ApiParam> innerParams = apiParamMap.get(ApiParamTypeConstant.INNER);
        if (!ObjectUtils.isEmpty(innerParams)){
            for (ApiParam param : innerParams) {
                String key = param.getTitle();
                Object paramValue = param.getDefaultVal();
                paramMap.put(key, paramValue);
            }
        }
        return paramMap;
    }
    protected Map afterQuery(Map<String, Object> result, ApiInfo apiInfo, Map<Integer,List<ApiParam>> apiParamMap) {
        List<ApiParam> resultParams = apiParamMap.get(ApiParamTypeConstant.OUTPUT);
        if(!ObjectUtils.isEmpty(resultParams)){
            Map<String,Object> resultMap = new HashMap<>();
            for (ApiParam resultParam : resultParams) {
                resultMap.put(resultParam.getTitle(),result.get(resultParam.getTitle()));
            }
            return resultMap;
        }
        return result;
    }



}
